package edu.wtbu.cs.androiddemo.adapter;


import android.app.Activity;
import android.content.Intent;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import edu.wtbu.cs.androiddemo.R;
import edu.wtbu.cs.androiddemo.activity.jump.GoodDetailActivity;

public class goodsListAdapter extends RecyclerView.Adapter<goodsListAdapter.ViewHolder> {

    private List<Good> mList;
    private Activity mActivity;
    private Good good;

    public goodsListAdapter(Activity activity, List<Good> list) {
        mList = list;
        mActivity = activity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final ViewHolder viewHolder = new ViewHolder(View.inflate(mActivity, R.layout.goods_list_items2, null));
        viewHolder.functionView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = viewHolder.getAdapterPosition();
                Intent intent = new Intent(mActivity, GoodDetailActivity.class);
                intent.putExtra("goodInfo", mList.get(adapterPosition));
                mActivity.startActivity(intent);
            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        good = mList.get(position);
        holder.imageView.setImageResource(good.getImage());
        holder.title.setText(good.getName());
        holder.price.setText(""+good.getPrice());
        holder.count.setText(good.getVolume()+"人付款");
        holder.address.setText(good.getProductLocation());
        holder.shopName.setText(good.getShopName());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView shopName;
        TextView title;
        TextView count;
        TextView address;
        TextView price;
        ImageView imageView;
        View functionView;

        public ViewHolder(View itemView) {
            super(itemView);
            functionView = itemView;
            imageView = itemView.findViewById(R.id.iv_iamge);
            title = (TextView) itemView.findViewById(R.id.tv_title);
            count = (TextView) itemView.findViewById(R.id.tv_count);
            address = (TextView) itemView.findViewById(R.id.tv_address);
            price = (TextView) itemView.findViewById(R.id.tv_new_price);
            shopName = (TextView) itemView.findViewById(R.id.tv_shopname);
        }

    }
}